package homework;

import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.rowset.CachedRowSet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/addRoleServlet.action")
public class AddRoleServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        Role role=new Role();
        try {


            BeanUtils.populate(role, req.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        HttpSession session = req.getSession();
        List<Role> roleList = (List<Role>) session.getAttribute("roleList");
        if(roleList==null){
            roleList=new ArrayList<>();
        }
        roleList.add(role);
        System.out.println(roleList);
        session.setAttribute("roleList", roleList);
        System.out.println(role);
        resp.sendRedirect(req.getContextPath()+"/day35homeWork/roleInfo.jsp");
    }
}
